<?php
/**
 * This file is part of the Pagizer package.
 *
 * For the full copyright and license information, please view the LICENSE
 * file that was distributed with this source code.
 *
 * @copyright   Copyright (c) 2010 Advisa (http://www.advisa.fr)
 * @author      Pagizer Core Team <team@pagizer.org>
 * @package     pagizer
 * @subpackage  medias
 */

/**
 * Check if the current user has right to launch the code between the tags relation to a document and a role.
 *
 * SEE function.hasDocRightElse.php FOR ELSE CONDITION !
 *
 * @param document : the document to check if user has right or not
 * @param module : the module from the dcoument
 * @param rule : front, read, edit or delete
 *
 * @example : {hasDocRight document=$media module='media' rule='show'}<h1>My title</h1><p>some news</p>{/hasDocRight}
 */
function smarty_block_hasDocRight($params, $content, &$smarty, &$repeat)
{		
	if($params['rule'] != "" && is_object($params['document']) && $params['module'] != "" )
	{
		$rules = explode(" ", $params['rule']);
        $document = $params['document'];
        $module = $params['module'];
		$ok = false;

        $context = isset($params["context"]) ? $params["context"] : "";
        $user = f_context_User::getInstance($context)->getUser();

        $else = $smarty->left_delimiter . 'hasDocRightElse' . $smarty->right_delimiter;

        $trueFalse = explode($else, $content, 2);

        $true = (isset($trueFalse[0]) ? $trueFalse[0] : null);
        $false = (isset($trueFalse[1]) ? $trueFalse[1] : null);

		foreach($rules as $rule)
		{
			//if the user has the right
			if($user->hasDocRight($document, $module, $rule) == true)
			{
				$ok = true;
			}
		}
        
        return ($ok == true) ? $true : $false;
	}
	else
	{		
		return $content;
	}
}